package com.mlz.mlzmall.ui.activity.act.personal;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.GetCustomerPointResponse;
import com.mlz.mlzmall.bean.GetMyExperienceResponse;
import com.mlz.mlzmall.bean.GetProductToExchangeListResponse;
import com.mlz.mlzmall.ui.activity.act.base.TopTitleView;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.VolleyBaseActivity;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;
import com.mlz.mlzmall.ui.utils.util.AppManager;
import com.mlz.mlzmall.ui.utils.util.ListViewUtils;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * Created by Pm on 2016/5/18.
 */
public class IntegralActivity extends VolleyBaseActivity {
    private GetCustomerPointResponse.CustomerPointDto data;//我的总积分
    private GetMyExperienceResponse.MyExperienceDto datas;
    private List<GetProductToExchangeListResponse.PageableDto.ProductToExchangeItemDto> pointdata;

    private TextView tv_integral;
    private Button bt_integralrecord;
    private Button bt_taxchangerecord;
    private TextView tv_name;
    private TextView tv_integration_again;
    private TextView tv_growthtechniques;

    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral); //跳转到我的积分界面
        AppManager.getAppManager().addActivity(this);
        initView();
        getData();
        //是否同步？
        getData3();
        setViewContent();
    }
    private void setViewContent() {
        TopTitleView loginTopTitle = (TopTitleView) findViewById(R.id.login_top_title);
        loginTopTitle.setLeftButtonContent(null, R.mipmap.pic_2_1_btn_back, null);
        loginTopTitle.setTitleButtonContent("我的积分", null, null);
        loginTopTitle.setOnLeftButtonClickListener(new TopTitleView.OnLeftButtonClickListener() {
            @Override
            public void onLeftBtnClick(View button) {
                finish();
                IntegralActivity.this.overridePendingTransition(R.anim.right_in,R.anim.right_out);
            }
        });
    }

    private void initView() {
        tv_integral = (TextView) findViewById(R.id.tv_integration_total_point);
        bt_integralrecord = (Button) findViewById(R.id.btn_credits_Log);
        bt_taxchangerecord = (Button) findViewById(R.id.btn_exchange_log);
        tv_name = (TextView) findViewById(R.id.btn_name);
        tv_integration_again = (TextView) findViewById(R.id.tv_integration_again);
        tv_growthtechniques = (TextView) findViewById(R.id.tv_growthtechniques);

        listView = (ListView) findViewById(R.id.lv_integration_picture);

        tv_growthtechniques.setOnClickListener(this);
        bt_integralrecord.setOnClickListener(this);
        bt_taxchangerecord.setOnClickListener(this);
    }

    public void getData() {
        tag = "IntegralActivity_getData";
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.POINT.INFO, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
              //  Toast.makeText(IntegralActivity.this, jsonObject.toString(), Toast.LENGTH_SHORT).show();
                Gson gson = new Gson();
                GetCustomerPointResponse bean = gson.fromJson(jsonObject.toString(), GetCustomerPointResponse.class);
                data = bean.getData();
                tv_integral.setText(data.getPointNum() + "");
                getData2();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(tag, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };
        // 设置该请求的标签
        jsonRequest.setTag(tag);
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);

    }

    public void getData2() {
        tag = "IntegralActivity_getData2";
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.EXP.MYExperience, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
          //      Toast.makeText(IntegralActivity.this, jsonObject.toString(), Toast.LENGTH_SHORT).show();
                Gson gson = new Gson();
                GetMyExperienceResponse bean = gson.fromJson(jsonObject.toString(), GetMyExperienceResponse.class);
                datas = bean.getData();
                tv_name.setText(datas.getExpName());
                tv_integration_again.setText(datas.getExpDescription());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(tag, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };
        // 设置该请求的标签
        jsonRequest.setTag(tag);
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);

    }
    //获取税换列表
    public void getData3()
    {
        tag = "IntegralActivity_getData3";
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.EXCHANGE.PRODUCTS, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                GetProductToExchangeListResponse bean = gson.fromJson(jsonObject.toString(),GetProductToExchangeListResponse.class);
                if(bean!=null)
                {
                    if(bean.getData()!=null&&bean.getData().size>0)
                    {
                        pointdata = bean.getData().getList();
                        if(pointdata!=null&&pointdata.size()>0)
                        {
                            listView.setAdapter(new MyAdapter());
                            ListViewUtils.setListViewHeightBasedOnChildren(listView,true);
                        }
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(tag, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };
        // 设置该请求的标签
        jsonRequest.setTag(tag);
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_growthtechniques:
                Intent intent = new Intent();
                intent.setClass(IntegralActivity.this, RankGrowthActivity.class);
                if(datas!=null) {
                intent.putExtra("levelDescriptionUrl", datas.getLevelDescriptionUrl());
            }
                startActivity(intent);
                break;
            case R.id.btn_credits_Log:
                Intent intent1 = new Intent();
                intent1.setClass(IntegralActivity.this, IntegralRecordActivity.class);
                startActivity(intent1);
                break;
            case R.id.btn_exchange_log:
                Intent intent2 = new Intent();
                intent2.setClass(IntegralActivity.this, ForRecordActivity.class);
                startActivity(intent2);
                break;
            default:
                break;
        }
    }
    //没优化
    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return pointdata.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.integration_product, null);
            ViewHolder vh = new ViewHolder();
            vh.iv_integretion_product = (ImageView)convertView.findViewById(R.id.iv_integretion_product);
            vh.tv_points = (TextView)convertView.findViewById(R.id.tv_points);
            vh.tv_title_integration = (TextView)convertView.findViewById(R.id.tv_title_integration);
            vh.iv_go_integration = (ImageView)convertView.findViewById(R.id.iv_go_integration);
            vh.iv_over_integration = (ImageView)convertView.findViewById(R.id.iv_over_integration);
            vh.ll_product_intergation =(LinearLayout)convertView.findViewById(R.id.ll_product_intergation);
            if (convertView != null) {
                vh.tv_title_integration.setText(pointdata.get(position).getProductName());//显示积分商品的名字
                vh.tv_points.setText(String.valueOf(pointdata.get(position).getPointsAmount()));//显示产品积分数
                imageLoader.displayImage(pointdata.get(position).getImage(), vh.iv_integretion_product, options);//显示商品的图片
                if (pointdata.get(position).getStatus().equals("EXPIRED")){//已过期
                    vh.iv_over_integration.setVisibility(View.VISIBLE);
                }else if (pointdata.get(position).getStatus().equals("AVAILABLE")){//可兑换
                    vh.iv_go_integration.setVisibility(View.VISIBLE);
                }else if (pointdata.get(position).getStatus().equals("STOCKOUT")){//已兑完

                }else if (pointdata.get(position).getStatus().equals("INSUFFICIENT_POINT")){//积分不足

                }
            }
            vh.ll_product_intergation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v){
                    Intent intent = new Intent(IntegralActivity.this,IntergationRedemptionActivity.class);
                    int StringId;
                    StringId = pointdata.get(position).getProductToExchangeId();//传入静态参数
               //     intent.putExtra("ProductToExchangeId",StringId );
                    SharedUtils.put(IntegralActivity.this,"ProductToExchangeId",StringId);
                    startActivity(intent);
                    overridePendingTransition(R.anim.in_from_right_back_center, R.anim.out_to_left_back_center);
//                  Toast.makeText(IntegralActivity.this,""+pointdata.get(position).getProductToExchangeId(),Toast.LENGTH_SHORT).show();
                }
            });
            return convertView;
        }
        class ViewHolder {
            ImageView iv_integretion_product;
            TextView tv_title_integration;
            TextView tv_points;
            ImageView iv_go_integration;
            ImageView iv_over_integration;
            LinearLayout ll_product_intergation;
        }
    }
}
